<script>
  function generateUUID(prefix = 's') {
    const timestamp = Date.now().toString(16)
    const random = Math.random().toString(16).slice(2)
    return `${prefix}${timestamp}${random}`
  }
  function fetch() {
    // 是否继续执行
    let isStop = false
    let key = '测试所用时长'
    function testFetch(name, time, isWatch = false) {
      let timer = null
      let loop = ({ resolve, reject }) => {
        console.info('runing loop.')
        if (isStop) {
          clearTimeout(timer)
          resolve(name)
          return
        }
        clearTimeout(timer)
        timer = setTimeout(() => {
          loop({ resolve, reject })
        }, 50)
      }
      return new Promise((resolve, reject) => {
        if (isWatch) {
          loop({ resolve, reject })
        } else {
          setTimeout(() => {
            resolve(name)
          }, time)
        }
      })
    }
    let stop = () => {
      isStop = true
    }
    let res = new Promise((resolve, reject) => {
      Promise.race([
        testFetch('线程1', 30000),
        testFetch('中断线程', null, true)
      ]).then((res) => {
        stop()
        console.timeEnd(key)
        resolve(res)
      })
    })
    res.stop = stop
    return res
  }
  let res = fetch()
  res.then((res) => {
    console.log('完成:', res)
  })
  // res.stop() 可以强制组织
</script>
